# LOAD PACKAGES
library(tidyverse)
library(maptools)
library(rgdal)
library(tmap)

#SET WORKING DIRECTORY
setwd("state_disengagement_rep/")

#READ IN DATA
colony_borders <- readRDS("state_disengagement_rep/maps/colony_borders.rds")
hostile_graph_df_stacked <- readRDS("state_disengagement_rep/maps/hostile_graph_df_stacked.rds")
map_pre1920 <- readRDS("state_disengagement_rep/maps/map_pre1920.rds")
map <- readRDS("state_disengagement_rep/maps/map.rds")

#MAKE FIGURE 1
tm_shape(map) +
  tm_polygons("InfrastructureExpenditures", title = "Mean Infrastructure \nExpenditures (Log)", border.alpha = 1) +
  tm_bubbles(size = "Head_Taxes_African_Quantile",scale=1,title.size="Head Taxes African Quantile")+
  tm_layout(frame = FALSE,legend.position=c(.75,.0005))+
  tm_shape(colony_borders)+
  tm_text("country",xmod=c(-6,2,-2,.9,0,0,-1.8,2), ymod=c(-2,-1.5,-2.5,-3.5,0,0,-3,1))+
  tm_borders("black")

#MAKE FIGURE 2
tm_shape(map) + 
  tm_polygons(c("hec_1920","hec_1930","hec_1940","hec_1950"),title="Mean Hostility Level"
              ,breaks=c(0,.5,1,2,4)
  ) +
  tm_facets( ncol = 2, sync = TRUE)+
  tm_shape(colony_borders)+
  tm_borders("Black")+
  tm_text("ccode")+
  tm_layout(legend.outside = TRUE,legend.outside.position=c("bottom", "center"),
            legend.title.size=1,
            legend.text.size=.75,
            panel.labels = c("1906-1920", "1920-1930","1930-1940","1940-1956"))

#MAKE FIGURE A.1
ggplot() + geom_bar(aes(y = hec_sum, x = colony_name, fill = year), data =hostile_graph_df_stacked,stat = "identity")+
  #ggtitle("Chieftan Resistance by Colony")+
  xlab("Colony")+
  ylab("Level of Perceived Hostility")+
  labs(x = "", fill = "Decade")+theme_light()+
  theme(text = element_text(size=15),
        axis.text.x = element_text(angle=45, hjust=1))

#MAKE FIGURE A.4
tm_shape(map_pre1920) + 
  tm_polygons("InfrastructureExpenditures", title = "Mean Infrastructure \nExpenditures (Log)", border.alpha = 1) +
  tm_layout(frame = FALSE)+
  tm_shape(colony_borders)+
  tm_text("ccode")+
  tm_borders("blue")

#MAKE FIGURE A.5
tm_shape(map_pre1920) + 
  tm_polygons("HeadTaxonAfricans", title = "Mean Head Tax on Africans (Log)", border.alpha = 1 ,breaks=c(0,11.30,12.03,12.60,14.10)) +
  tm_layout(frame = FALSE)+
  tm_shape(colony_borders)+
  tm_text("ccode")+
  tm_borders("blue")

